package com.example.mvvm.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.mvvm.api.VideoEntity
import com.example.mvvm.base.BaseViewModel
import com.example.mvvm.base.Default
import com.example.mvvm.base.Empty
import com.example.mvvm.base.Failed
import com.example.mvvm.base.Loading
import com.example.mvvm.base.RequestEvent
import com.example.mvvm.base.Success
import com.example.mvvm.respository.MainRepository
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.count
import kotlinx.coroutines.launch

/**
 * livedata/ShareFlow/StateFlow有什么区别？
 * 冷流/热流有什么区别？
 */
class MainViewModel:BaseViewModel() {
    //方案一：livedata
    val data1 = MutableLiveData<MutableList<VideoEntity>>()
    //方案二：ShareFlow
    private val _data2 = MutableSharedFlow<RequestEvent>() //可变，只允许本类
    val data2:SharedFlow<RequestEvent> = _data2.asSharedFlow() //不可变，只允许外部访问

    //方案三：StateFlow
    private val _data3 = MutableStateFlow<RequestEvent>(Default) //可变，只允许本类，默认值
    val data3:StateFlow<RequestEvent> = _data3.asStateFlow() //不可变，只允许外部访问


    private val repository:MainRepository by lazy{
        MainRepository()
    }

    fun getVideoList(params:Map<String,String>){
        execute(
            action = repository.getVideoList(params),
            onStart = {
                _data3.value = Loading
            },
            onFinished = {
                if(it.data?.isNotEmpty() == true){
                    _data3.value = Success
                }else{
                    _data3.value = Empty
                }
            },
            onError = {
                _data3.value = Failed(it)
            }

        )
    }



//    fun getVideoList(params:Map<String,String>){
//        viewModelScope.launch {
//            _data3.value = Loading
//            repository.getVideoList(params)
//                .catch { err->
//                    _data3.value = Failed(err.message.toString())
//                }
//                .collect{
//                if(it.code == 200){
//                    if(it.data?.isNotEmpty() == true){
//                        _data3.value = Success
//                    }else{
//                        _data3.value = Empty
//                    }
//                }else{
//                    //失败：统一返回
//                    _data3.value = Failed(it.message)
//                }
//            }
//        }
//    }





    //协程作用域
//    fun getVideoList(params:Map<String,String>){
//        viewModelScope.launch {
//            repository.getVideoList(params).collect{
//                if(it.code == 200){
//                    //方案一：livedata
//                    data1.postValue(it.data)
//                    //方案二：ShareFlow
//                    it.data?.let { it1 -> _data2.emit(it1) }
//                    //方案三：StateFlow
//                    _data3.value = it.data
//                }else{
//                    //失败：统一返回
//                }
//            }
//
//
//        }
//
//
//
//    }


}